<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          size="small"
          placeholder="请输入系统名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation show="" :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form ref="form" :inline="true" :model="form" size="small" label-width="120px">
        <el-form-item label="系统名称" prop="name" :rules=" [{ required: true, message: '请输入接入系统名称', trigger: 'blur' }]">
          <el-input v-model="form.name" placeholder="请输入接入系统名称" style="width: 450px" />
        </el-form-item>
        <el-form-item label="系统编码" prop="bm" :rules=" [{ required: true, message: '请输入接入系统编码', trigger: 'blur' }]">
          <el-input v-model="form.bm" placeholder="请输入接入系统编码" style="width: 450px" />
        </el-form-item>
        <el-form-item label="系统地址" prop="url" :rules=" [{ required: true, message: '请输入系统地址', trigger: 'blur' }]">
          <el-input v-model="form.url" placeholder="请输入系统地址" style="width: 450px" />
        </el-form-item>
        <el-form-item label="首页路径" prop="indexUrl" :rules=" [{ required: true, message: '请输入首页路径', trigger: 'blur' }]">
          <el-input v-model="form.indexUrl" placeholder="请输入首页路径" style="width: 450px" />
        </el-form-item>
        <el-form-item label="登录路径" prop="loginUrl" :rules=" [{ required: true, message: '请输入登录路径', trigger: 'blur' }]">
          <el-input v-model="form.loginUrl" placeholder="请输入登录路径" style="width: 450px" />
        </el-form-item>
        <el-form-item label="退出地址路径" prop="logoutUrl" :rules=" [{ required: true, message: '请输入退出地址路径', trigger: 'blur' }]">
          <el-input v-model="form.logoutUrl" placeholder="请输入退出地址路径" style="width: 450px" />
        </el-form-item>
        <el-form-item label="数据源" prop="sjy">
          <el-select v-model="form.sjy" placeholder="请选择数据源" style="width: 450px">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-popover
            placement="bottom-start"
            width="450"
            trigger="click"
            @show="$refs['iconSelect'].reset()"
          >
            <IconSelect ref="iconSelect" @selected="selected" />
            <el-input slot="reference" v-model="form.icon" style="width: 450px;" placeholder="点击选择图标" readonly>
              <svg-icon
                v-if="form.icon"
                slot="prefix"
                :icon-class="form.icon"
                class="el-input__icon"
                style="height: 32px;width: 32px;"
              />
              <i v-else slot="prefix" class="el-icon-search el-input__icon" />
            </el-input>
          </el-popover>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.isEnable">
            <el-radio
              v-for="item in dict.user_status"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column :show-overflow-tooltip="true" prop="name" label="接入系统名称" />
      <el-table-column :show-overflow-tooltip="true" prop="bm" label="系统编码" />
      <el-table-column :show-overflow-tooltip="true" prop="url" label="接入系统地址" />
      <el-table-column :show-overflow-tooltip="true" prop="icon" label="图标">
        <template slot-scope="scope">
          <svg-icon
            v-if="scope.row.icon"
            slot="prefix"
            :icon-class="scope.row.icon"
            class="el-input__icon"
            style="height: 32px;width: 32px;"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isEnable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.isEnable)"
          />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="createTime" width="135" label="创建日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="115"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :disabled-dle="scope.row.id === user.id"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import usystem from '@/api/system/usystem'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import IconSelect from '@/components/IconSelect'
import { requestGet } from '@/api/system/common'
const defaultForm = {
  id: null, wsdl: '', sjy: null, name: null, bm: null, url: null, icon: null, px: 0, isEnable: 'true', logoutUrl: '', indexUrl: '', loginUrl: ''
}
export default {
  name: 'ManagerSystem',
  components: { crudOperation, rrOperation, udOperation, pagination, IconSelect },
  cruds() {
    return CRUD({ title: '接入系统管理', url: 'rest/api/manager/system', crudMethod: { ...usystem }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 数据字典
  dicts: ['user_status'],
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  data() {
    return {
      permission: {
        add: ['admin', 'usystem:add'],
        edit: ['admin', 'usystem:edit'],
        del: ['admin', 'usystem:del']
      },
      options: []
    }
  },
  methods: {
    [CRUD.HOOK.afterAddError](crud) {
      this.afterErrorMethod(crud)
    },
    [CRUD.HOOK.afterEditError](crud) {
      this.afterErrorMethod(crud)
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      form.isEnable = form.isEnable.toString()
      this.initAllDataSource()
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.user_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        usystem.edit(data).then(res => {
          this.crud.notify(this.dict.label.user_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.enabled = !data.enabled
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    },
    selected(name) {
      this.form.icon = name
    },
    initAllDataSource() {
      requestGet('rest/api/manager/portal/v1/datasource/manager/blurry', {})
        .then(res => {
          const { data } = res
          this.options = data.data
        }).catch(() => {
          this.options = []
        })
    }
  }
}
</script>

<style scoped>

</style>
